﻿'Programmé par Michel Gagnon
Public Class GestionBeneficiaire
    Dim oLocalContext As DBMainContext
    Dim localBeneficiaire As Beneficiaire
    Dim oBeneModif As Beneficiaire
    Dim ajout As Boolean
    Dim modif As Boolean
    Dim lecture As Boolean

    Sub New()
        Me.New(New Beneficiaire)
        btnModifier.Image = My.Resources.Images.disk_blue
        Disponible()
        ajout = True
        lecture = False
    End Sub
    Function validation() As Boolean
        Dim valide As Boolean = True

        '----vérification sur le nom----
        If txtNom.Text.Trim = "" Then
            Outils.ErrorProviderFunctions.AddError(Me, txtNom, My.Resources.MessageErreur.DoitContenirUneValeur)
            valide = False
        Else
            Outils.ErrorProviderFunctions.RemoveError(Me, txtNom)

            '----regex----
            If Not Outils.Validation.CheckRegex(Outils.Validation.TypeRegex.Nom, txtNom.Text) Then
                Outils.ErrorProviderFunctions.AddError(Me, txtNom, My.Resources.MessageErreur.ErreurNom)
                valide = False
            Else
                Outils.ErrorProviderFunctions.RemoveError(Me, txtNom)
            End If
        End If

        '----vérification sur le prenom----
        If txtPrenom.Text.Trim = "" Then
            Outils.ErrorProviderFunctions.AddError(Me, txtPrenom, My.Resources.MessageErreur.DoitContenirUneValeur)
            valide = False
        Else
            Outils.ErrorProviderFunctions.RemoveError(Me, txtPrenom)

            '----regex----
            If Not Outils.Validation.CheckRegex(Outils.Validation.TypeRegex.Nom, txtPrenom.Text) Then
                Outils.ErrorProviderFunctions.AddError(Me, txtPrenom, My.Resources.MessageErreur.ErreurPrenom)
                valide = False
            Else
                Outils.ErrorProviderFunctions.RemoveError(Me, txtPrenom)
            End If
        End If

        '----vérification sur le stade----
        If cbStade.Text = "" Then
            Outils.ErrorProviderFunctions.AddError(Me, cbStade, My.Resources.MessageErreur.DoitContenirUneValeur)
            valide = False
        Else
            Outils.ErrorProviderFunctions.RemoveError(Me, cbStade)
        End If


        '----vérification sur le type----
        If cbType.Text = "" Then
            Outils.ErrorProviderFunctions.AddError(Me, cbType, My.Resources.MessageErreur.DoitContenirUneValeur)
            valide = False
        Else
            Outils.ErrorProviderFunctions.RemoveError(Me, cbType)
        End If



        '----vérification sur l'adresse----
        If txtAdresse.Text.Trim = "" Then
            Outils.ErrorProviderFunctions.AddError(Me, txtAdresse, My.Resources.MessageErreur.DoitContenirUneValeur)
            valide = False
        Else
            Outils.ErrorProviderFunctions.RemoveError(Me, txtAdresse)

            '----regex----
            If Not Outils.Validation.CheckRegex(Outils.Validation.TypeRegex.adresse, txtAdresse.Text) Then
                Outils.ErrorProviderFunctions.AddError(Me, txtAdresse, My.Resources.MessageErreur.ErreurAdresse)
                valide = False
            Else
                Outils.ErrorProviderFunctions.RemoveError(Me, txtAdresse)
            End If
        End If


        '----vérification sur le codePostal----
        If txtCodePostal.Text.Trim = "" Then
            Outils.ErrorProviderFunctions.AddError(Me, txtCodePostal, My.Resources.MessageErreur.DoitContenirUneValeur)
            valide = False
        Else
            Outils.ErrorProviderFunctions.RemoveError(Me, txtCodePostal)

            '----regex----
            If Not Outils.Validation.CheckRegex(Outils.Validation.TypeRegex.CodePostalQc, txtCodePostal.Text) Then
                Outils.ErrorProviderFunctions.AddError(Me, txtCodePostal, My.Resources.MessageErreur.ErreurCodePostal)
                valide = False
            Else
                Outils.ErrorProviderFunctions.RemoveError(Me, txtCodePostal)
            End If
        End If


        '----vérification sur la ville----
        If cbVille.Text.Trim = "" Then
            Outils.ErrorProviderFunctions.AddError(Me, cbVille, My.Resources.MessageErreur.DoitContenirUneValeur)
            valide = False
        Else
            Outils.ErrorProviderFunctions.RemoveError(Me, cbVille)

            '----regex----
            If Not Outils.Validation.CheckRegex(Outils.Validation.TypeRegex.Nom, cbVille.Text) Then
                Outils.ErrorProviderFunctions.AddError(Me, cbVille, My.Resources.MessageErreur.ErreurVille)
                valide = False
            Else
                Outils.ErrorProviderFunctions.RemoveError(Me, cbVille)
            End If
        End If


        '----vérification sur la province----
        If cbProvince.Text.Trim = "" Then
            Outils.ErrorProviderFunctions.AddError(Me, cbProvince, My.Resources.MessageErreur.DoitContenirUneValeur)
            valide = False
        Else
            Outils.ErrorProviderFunctions.RemoveError(Me, cbProvince)

            '----regex----
            If Not Outils.Validation.CheckRegex(Outils.Validation.TypeRegex.Nom, cbProvince.Text) Then
                Outils.ErrorProviderFunctions.AddError(Me, cbProvince, My.Resources.MessageErreur.ErreurProvince)
                valide = False
            Else
                Outils.ErrorProviderFunctions.RemoveError(Me, cbProvince)
            End If
        End If

        '----vérification sur les téléphone----

        If Not (Not txtCell.Text = "(   )    -" Or Not txtMaison.Text = "(   )    -") Then
            Outils.ErrorProviderFunctions.AddError(Me, txtCell, My.Resources.MessageErreur.DoitContenirTelephone)
            Outils.ErrorProviderFunctions.AddError(Me, txtMaison, My.Resources.MessageErreur.DoitContenirTelephone)
            valide = False
        Else
            Outils.ErrorProviderFunctions.RemoveError(Me, txtCell)
            Outils.ErrorProviderFunctions.RemoveError(Me, txtMaison)

            '----Regex----
            If Not Outils.Validation.CheckRegex(Outils.Validation.TypeRegex.TelQuebec, txtCell.Text) And Not txtCell.Text = "(   )    -" Then
                Outils.ErrorProviderFunctions.AddError(Me, txtCell, My.Resources.MessageErreur.FormatTelQuebec)
                valide = False
            Else
                Outils.ErrorProviderFunctions.RemoveError(Me, txtCell)
            End If

            If Not Outils.Validation.CheckRegex(Outils.Validation.TypeRegex.TelQuebec, txtMaison.Text) And Not txtMaison.Text = "(   )    -" Then
                Outils.ErrorProviderFunctions.AddError(Me, txtMaison, My.Resources.MessageErreur.FormatTelQuebec)
                valide = False
            Else
                Outils.ErrorProviderFunctions.RemoveError(Me, txtMaison)
            End If
        End If

        '----vérification courriel----
        If txtCourriel.Text.Trim = "" Then
            Outils.ErrorProviderFunctions.AddError(Me, txtCourriel, My.Resources.MessageErreur.DoitContenirUneValeur)
            valide = False
        Else
            Outils.ErrorProviderFunctions.RemoveError(Me, txtCourriel)

            '----regex----
            If Not Outils.Validation.CheckRegex(Outils.Validation.TypeRegex.Courriel, txtCourriel.Text) Then
                Outils.ErrorProviderFunctions.AddError(Me, txtCourriel, My.Resources.MessageErreur.ErreurCourriel)
                valide = False
            Else
                Outils.ErrorProviderFunctions.RemoveError(Me, txtCourriel)
            End If
        End If

        '----vérification de la validiter de tout les chanps----
        If valide = True Then
            Return True
        Else
            Return False
        End If

    End Function
    Sub Disponible()
        txtNom.Enabled = True
        txtPrenom.Enabled = True
        cbStade.Enabled = True
        cbType.Enabled = True
        dtpDateNaiss.Enabled = True
        dtpDernEval.Enabled = True
        txtAdresse.Enabled = True
        txtCodePostal.Enabled = True
        cbVille.Enabled = True
        cbProvince.Enabled = True
        txtCell.Enabled = True
        txtMaison.Enabled = True
        txtCourriel.Enabled = True
        txtNote.Enabled = True
        rbHomme.Enabled = True
        rbFemme.Enabled = True
        btnAjoutPhoto.Enabled = True

    End Sub
    Sub Indisponible()
        txtNom.Enabled = False
        txtPrenom.Enabled = False
        cbStade.Enabled = False
        cbType.Enabled = False
        dtpDateNaiss.Enabled = False
        dtpDernEval.Enabled = False
        txtAdresse.Enabled = False
        txtCodePostal.Enabled = False
        cbVille.Enabled = False
        cbProvince.Enabled = False
        txtCell.Enabled = False
        txtMaison.Enabled = False
        txtCourriel.Enabled = False
        txtNote.Enabled = False
        rbHomme.Enabled = False
        rbFemme.Enabled = False
        btnAjoutPhoto.Enabled = False
    End Sub
    Sub New(ByVal oBenef As Beneficiaire)
        InitializeComponent()
        oLocalContext = New DBMainContext
        If oBenef.ID = 0 Then
            localBeneficiaire = oBenef
            btnPersResc.Enabled = False
            btnInteret.Enabled = False
        Else
            localBeneficiaire = (From u In oLocalContext.Beneficiaire Where u.ID = oBenef.ID Select u).FirstOrDefault
            btnPersResc.Enabled = True
            btnInteret.Enabled = True
        End If

        cbType.DataSource = oLocalContext.Type_Beneficiaire
        cbType.DisplayMember = "Nom_Type"
        initialize()
        lecture = True
        Indisponible()
        If oBenef.Actif = False Then
            lblInactif.Visible = True
            btnSupprimer.Image = My.Resources.Images.user1_back
            btnSupprimer.Tag = "inactif"
        Else
            lblInactif.Visible = False
        End If
        'cbVille.DataSource = (From u As Beneficiaire In oLocalContext.Beneficiaire Select u.Ville).ToList() '.AddRange((From v In oLocalContext.Employe Select v.Ville).ToList)
        ' cbVille.DisplayMember = "Ville"
        'cbVille.ValueMember = "Ville"
    End Sub
    Sub initialize()
        dtpDateNaiss.MaxDate = Date.Today
        dtpDernEval.MaxDate = Date.Today
        cbVille.Load()
        cbProvince.Load()
        txtNom.Text = localBeneficiaire.Nom
        txtPrenom.Text = localBeneficiaire.Prenom
        cbStade.SelectedIndex = localBeneficiaire.Stade
        cbType.SelectedItem = localBeneficiaire.Type_Beneficiaire

        If localBeneficiaire.DateNaissance Is Nothing Then
            dtpDateNaiss.Value = Date.Today

        Else
            dtpDateNaiss.Value = localBeneficiaire.DateNaissance
        End If

        If localBeneficiaire.DerniereEvaluation Is Nothing Then
            dtpDernEval.Value = Date.Today
        Else
            dtpDernEval.Value = localBeneficiaire.DerniereEvaluation
        End If

        txtAdresse.Text = localBeneficiaire.Adresse
        txtCodePostal.Text = localBeneficiaire.CodePostal
        cbVille.SelectedText = localBeneficiaire.Ville
        cbProvince.SelectedText = localBeneficiaire.Province
        txtCell.Text = localBeneficiaire.TelCellulaire
        txtMaison.Text = localBeneficiaire.TelMaison
        txtCourriel.Text = localBeneficiaire.Courriel
        txtNote.Text = localBeneficiaire.Note

        If Not localBeneficiaire.Photo Is Nothing Then
            pbPhoto.Image = Outils.GestionImage.GetImageFromDB(localBeneficiaire.Photo)
            pbPhoto.SizeMode = PictureBoxSizeMode.StretchImage
        Else
            pbPhoto.Image = My.Resources.Images.NoPhoto_user
            pbPhoto.SizeMode = PictureBoxSizeMode.StretchImage
        End If

        If localBeneficiaire.Sexe Is Nothing Then
            localBeneficiaire.Sexe = "H"
        End If
        If localBeneficiaire.Sexe = "H" Then
            rbHomme.Checked = True
        ElseIf localBeneficiaire.Sexe = "F" Then
            rbFemme.Checked = True
        End If

    End Sub
    Private Sub GestionBeneficiaire_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    End Sub
    Private Sub btnModifier_Click(sender As System.Object, e As System.EventArgs) Handles btnModifier.Click

        If ajout = True Then

            If validation() = True Then

                With localBeneficiaire
                    .Nom = txtNom.Text.Trim
                    .Prenom = txtPrenom.Text.Trim
                    .Stade = cbStade.SelectedIndex
                    .Type_Beneficiaire = CType(cbType.SelectedItem, Type_Beneficiaire)
                    .DateNaissance = dtpDateNaiss.Value
                    .DerniereEvaluation = dtpDernEval.Value
                    .Adresse = txtAdresse.Text.Trim
                    .CodePostal = txtCodePostal.Text.Trim
                    .Ville = cbVille.Text.Trim
                    .Province = cbProvince.Text.Trim
                    .TelCellulaire = txtCell.Text.Trim
                    .TelMaison = txtMaison.Text.Trim
                    .Courriel = txtCourriel.Text.Trim
                    .Note = txtNote.Text.Trim

                    If rbHomme.Checked = True Then
                        localBeneficiaire.Sexe = "H"
                    ElseIf rbFemme.Checked = True Then
                        localBeneficiaire.Sexe = "F"
                    End If
                    .Actif = True 
                End With

                oLocalContext.Beneficiaire.InsertOnSubmit(localBeneficiaire)
                oLocalContext.SubmitChanges()
                btnModifier.Image = My.Resources.Images.edit
                Indisponible()
                ajout = False
                lecture = True
                btnPersResc.Enabled = True
                btnInteret.Enabled = True
            End If

        ElseIf lecture = True Then
            Disponible()
            btnModifier.Image = My.Resources.Images.disk_blue
            lecture = False
            modif = True
            btnSupprimer.Enabled = True
        ElseIf modif = True Then

            If validation() = True Then
                oBeneModif = (From u In oLocalContext.Beneficiaire Where u.ID = localBeneficiaire.ID Select u).FirstOrDefault

                With oBeneModif
                    .Nom = txtNom.Text.Trim
                    .Prenom = txtPrenom.Text.Trim
                    .Stade = cbStade.SelectedIndex
                    .Type_Beneficiaire = CType(cbType.SelectedItem, Type_Beneficiaire)
                    .DateNaissance = dtpDateNaiss.Value
                    .DerniereEvaluation = dtpDernEval.Value
                    .Adresse = txtAdresse.Text.Trim
                    .CodePostal = txtCodePostal.Text.Trim
                    .Ville = cbVille.Text.Trim
                    .Province = cbProvince.Text.Trim
                    .TelCellulaire = txtCell.Text.Trim
                    .TelMaison = txtMaison.Text.Trim
                    .Courriel = txtCourriel.Text.Trim
                    .Note = txtNote.Text.Trim

                    If rbHomme.Checked = True Then
                        .Sexe = "H"
                    ElseIf rbFemme.Checked = True Then
                        .Sexe = "F"
                    End If

                End With
                btnSupprimer.Enabled = False

                oLocalContext.SubmitChanges()
                modif = False
                Indisponible()
                btnModifier.Image = My.Resources.Images.edit
                lecture = True
            End If
            End If
    End Sub
    Private Sub btnPersResc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPersResc.Click
        Dim oNewPersResc As New ListePersonneRessource(localBeneficiaire, oLocalContext)
        MainForm.AddTabToTC("Personne(s) Ressource(s)/" + localBeneficiaire.Nom + "," + localBeneficiaire.Prenom, True, oNewPersResc)
    End Sub
    Private Sub btnSupprimer_Click(sender As System.Object, e As System.EventArgs) Handles btnSupprimer.Click
        If btnSupprimer.Tag = "actif" Then
            localBeneficiaire.Actif = False
            lblInactif.Visible = True   
            btnSupprimer.Image = My.Resources.Images.user1_back
            btnSupprimer.Tag = "inactif"
        ElseIf btnSupprimer.Tag = "inactif" Then
            localBeneficiaire.Actif = True
            lblInactif.Visible = False
            btnSupprimer.Image = My.Resources.Images.user1_delete
        End If

    End Sub

    Private Sub txtNom_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNom.Validated
        txtNom.Text = Outils.Validation.CapFirstChar(txtNom.Text)
    End Sub

    Private Sub txtPrenom_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPrenom.Validated
        txtPrenom.Text = Outils.Validation.CapFirstChar(txtPrenom.Text)
    End Sub

    Private Sub btnAjoutPhoto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAjoutPhoto.Click
        Dim photoid As Integer
        Dim strPhoto As String

        strPhoto = Outils.GestionImage.GetImageFromFileDialog()
        photoid = Outils.GestionImage.SaveImageToDB(strPhoto)
        localBeneficiaire.Photo = photoid
        pbPhoto.Image = Outils.GestionImage.GetImageFromDB(photoid)
        pbPhoto.SizeMode = PictureBoxSizeMode.StretchImage
    End Sub

    Private Sub btnFermer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFermer.Click
        Outils.TabControl.RemoveSelectedTab()
    End Sub

    Private Sub btnInteret_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInteret.Click
        Dim oNewInteretFormation As New FormationInteret(localBeneficiaire)
        Dim ocust As New CustomMessageBox
        ocust.Height = oNewInteretFormation.Height + 90
        ocust.Width = oNewInteretFormation.Width
        ocust.Controls.Add(oNewInteretFormation)

        ocust.ShowDialog()
    End Sub
End Class
